package com.zpf.dao.impl;

import com.zpf.dao.OrderDao;
import com.zpf.pojo.Address;
import com.zpf.pojo.Order;
import com.zpf.utils.DataSourceUtils;
import com.zpf.utils.TxUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class OrderDaoImpl implements OrderDao {
    @Override
    public long findCountByAid(String id) {
        QueryRunner qf = new QueryRunner(DataSourceUtils.getDataSource());

        String sql = "select count(0) from tb_order where aid = ? ";
        try {
          long count = (long) qf.query(sql, new ScalarHandler(),id);
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("查询订单数量失败");
        }
        return 0;
    }

    @Override
    public void insert(Order order) {
        Connection conn = TxUtils.getConn();
        QueryRunner qf = new QueryRunner();
        String sql = "INSERT INTO `tb_order` (`id`, `uid`, `money`, `status`, `time`, `aid`) VALUES (?, ?, ?, ?, ?, ?)";
        try {
            qf.update(conn,sql,order.getId(),order.getUid(),order.getMoney(),order.getStatus(),order.getTime(),order.getAid());
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("添加订单失败");
        }

    }

    @Override
    public void paySuccess(Order order) {
        Connection conn = TxUtils.getConn();
        QueryRunner qf = new QueryRunner();
        String sql = "update tb_order set status = ? where id = ?";
        try {
            qf.update(conn,sql,order.getStatus(),order.getId());
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("支付失败");
        }
    }
}
